package com.toshi.netlib.api;


import android.text.TextUtils;
import android.util.Log;
import com.toshi.netlib.base.NetworkApi;
import com.toshi.netlib.errorhandler.ExceptionHandle;
import io.reactivex.functions.Function;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * netApi的实例
 * 查询物品的接口提供商的api
 * 示例实现  防止完全掌握之前忘记
 */
public class Ff14MidApi extends NetworkApi {

    private static volatile Ff14MidApi instance;

    public static Ff14MidApi getInstance() {
        if (instance == null) {
            synchronized (Ff14MidApi.class) {
                if (instance == null) {
                    instance = new Ff14MidApi();
                }
            }
        }
        return instance;
    }

    public static <T> T getService(Class<T> service) {
        return getInstance().getRetrofit(service).create(service);
    }

    @Override
    protected Interceptor getInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                return chain.proceed(request);
            }
        };
    }

    @Override
    protected <T> Function<T, T> getAppErrorHandler() {
        return new Function<T, T>() {
            @Override
            public T apply(T response) throws Exception {
                if (response instanceof BaseResponse && ((BaseResponse) response).getCode() != 0) {
                    ExceptionHandle.ServerException exception = new ExceptionHandle.ServerException();
                    exception.code = ((BaseResponse) response).getCode();
                    exception.message = TextUtils.isEmpty(((BaseResponse) response).getMsg()) ? "hhhhhh" : ((BaseResponse) response).getMsg();
                    throw exception;
                }
                return response;
            }
        };
    }

    @Override
    public String getFormal() {
        return "https://cafemaker.wakingsands.com/";
    }

    @Override
    public String getTest() {
        return "https://cafemaker.wakingsands.com/";
    }
}
